﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OAuth;
using System.Web.Script.Serialization;
using Darren.Common.Helper;

public partial class SinaDemo : System.Web.UI.Page
{
    SinaOAuth auth = new SinaOAuth();
    protected void Page_Load(object sender, EventArgs e)
    {
        string oauth = CookieHelper.GetCookieValue("SinaOAuth");
        string token = StringHelper.QueryString(oauth, "Token");
        string token_secret = StringHelper.QueryString(oauth, "TokenSecret");

        Response.Write(string.Format("access_oauth_token={0}<br/>access_oauth_token_secret={1}<br/>access_openid={2}<br/>",
        Session["access_oauth_token"],
        Session["access_oauth_token_secret"],
        Session["access_openid"]));

        auth.UserRemoteIP = Request.UserHostAddress;
        auth.Token = new AccessToken(Session["access_oauth_token"].ToString(), Session["access_oauth_token_secret"].ToString(), Session["access_openid"].ToString());
    }

    protected void getfavorites_Click(object sender, EventArgs e)
    {
        string info = auth.GetFavorites(1);
        TextBox1.Text = info;
    }
    protected void getWeeklyHotTopic_Click(object sender, EventArgs e)
    {
        string info = auth.GetWeeklyHotTopic();
        TextBox1.Text = info;
    }
    protected void btnGetSinaInfo_Click(object sender, EventArgs e)
    {

        MemberInfo info = auth.GetUserInfo();
        Response.Write(string.Format("{0}:{1}<br/><img src='{2}'/>", info.MemberID, info.DisplayName, info.ImageUrl));
    }

    protected void getFaces_Click(object sender, EventArgs e)
    {
        string info = auth.GetFaces();
        TextBox1.Text = info;
    }

    protected void getInteretedUser_Click(object sender, EventArgs e)
    {
        string info = auth.GetInterestedUsers();
        TextBox1.Text = info;
    }
    protected void getSinaFriendInfo_Click(object sender, EventArgs e)
    {
        string info = auth.GetFriendInfo();
        TextBox1.Text = info;
    }
    protected void btnUpdateSina_Click(object sender, EventArgs e)
    {
        string content = txtSinaStatus.Text;
        string info = auth.UpdateStatus(content);
        TextBox1.Text = info;
    }
    protected void getFriendsIDs_Click(object sender, EventArgs e)
    {
        string uid = txtFindFriendUID.Text;

        string info = auth.GetFriends(uid);

        TextBox1.Text = info;
    }
    protected void FriendCreate_Click(object sender, EventArgs e)
    {
        string info = auth.FriendshipsCreate(txtCreateUID.Text);
        TextBox1.Text = info;
    }
    protected void btnSaveFriends_Click(object sender, EventArgs e)
    {
        //string rootUID = txtRootUID.Text;
        //int count = int.Parse(txtSaveCount.Text);
        //string responseData = auth.GetFriends(rootUID);
        //FriendsList list = new JavaScriptSerializer().Deserialize<FriendsList>(responseData);
        //List<long> result = new List<long>();

        //foreach (var l in list.ids)
        //{
        //    saveFriends(l.ToString(), result);
        //}
        //try
        //{
        //    foreach (var l in result)
        //    {
        //        Response.Write(l.ToString() + ",");
        //        Response.Flush();
        //        auth.FriendshipsCreate(l.ToString());
        //    }
        //}
        //catch (Exception ex)
        //{
        //    //Response.Write(ex + ",");
        //    //Response.Flush();
        //}
    }

    delegate bool breakfun();

    private void saveFriends(string uid,List<long> list)
    {
        //string responseData = auth.GetFriends(uid);
        //FriendsList _list = new JavaScriptSerializer().Deserialize<FriendsList>(responseData);

        //foreach (var l in _list.ids)
        //{
        //    if (list.Count>2000)
        //    {
        //        break;
        //    }
        //    list.Add(l);
        //}
    }
}